package com.teamclub.erp.scontrollers

import java.io.{BufferedOutputStream, File, FileInputStream, FileOutputStream}

import com.google.common.io.Files
import com.teamclub.erp.sforms.StorageRecordForm
import com.teamclub.erp.sservices.StorageRecordService
import com.teamclub.util.libs.{Eithers, Libs}
import org.apache.commons.io.IOUtils
import org.apache.commons.lang3.StringUtils
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.{HttpHeaders, HttpStatus, MediaType, ResponseEntity}
import org.springframework.web.bind.annotation._
import org.springframework.web.multipart.MultipartFile

/**
  * Created by ilkkzm on 17-9-11.
  */
@RestController("com.teamclub.erp.scontrollers.StorageRecordController")
@RequestMapping(Array("/erp/storagerecord"))
class StorageRecordController {
  private var logger = LoggerFactory.getLogger(classOf[StorageRecordController])
  @Autowired
  var storageRecordService: StorageRecordService = _

  @PostMapping(Array("list"))
  def list(@RequestBody form: StorageRecordForm) = {
    Eithers.toJson(storageRecordService.list(form))
  }

  @PostMapping(Array("detailList"))
  def detailList(@RequestBody form: StorageRecordForm) = {
    Eithers.toJson(storageRecordService.detailList(form))
  }
  @PostMapping(Array("save"))
  def add(@RequestBody storageRecordForm: StorageRecordForm) = {
    val resp = storageRecordService.add(storageRecordForm)
    Eithers.toJson(resp)
  }
  @PostMapping(Array("upload"))
  def upload(@RequestParam("file") multifile: MultipartFile) = {
    val file = Libs.getTempFile(multifile.getOriginalFilename)
    val outputStream = new BufferedOutputStream(new FileOutputStream(file))
    outputStream.write(multifile.getBytes)
    outputStream.flush()
    outputStream.close()
    val resp = storageRecordService.upload(file)
    Eithers.toJson(resp)
  }
  @PostMapping(Array("download"))
  def download(@RequestBody form: StorageRecordForm): ResponseEntity[Array[Byte]] = {
    if(StringUtils.isBlank(form.getFileName)) {
      return new ResponseEntity[Array[Byte]](HttpStatus.BAD_REQUEST)
    }
    val resp = storageRecordService.download(form)
    if(resp.left.isDefined) {
      var inputStream:FileInputStream = null
      try {
        inputStream = new FileInputStream(resp.left.get)
        val bytes: Array[Byte] = IOUtils.toByteArray(new FileInputStream(resp.left.get))
        val headers = new HttpHeaders
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM)
        val fileName = form.fileName
        headers.setContentDispositionFormData(fileName, fileName)
        headers.setCacheControl("must-revalidate, post-check=0, pre-check=0")
        new ResponseEntity[Array[Byte]](bytes, headers, HttpStatus.OK)
      } catch {
        case e: Exception => {
          logger.error("", e)
          return new ResponseEntity[Array[Byte]](HttpStatus.BAD_REQUEST)
        }
      } finally {
        if(inputStream != null) {
          inputStream.close
        }
      }
    } else {
      new ResponseEntity[Array[Byte]](HttpStatus.BAD_REQUEST)
    }
  }
}
